<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<%@page import="java.util.List" %>
<%@page import="com.sql.Sql" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Struts - Musica</title>
        <style type="text/css">
            @import "/musica/musica.css";
        </style>
        <%
        String idCategoria = request.getParameter("idCategoria");
        String idCd = request.getParameter("idCd");
        %>
    </head>
    <body>
        <p class="titulo">Temas en CD</p>

        <%
        out.println("<form action='temasEnCd.jsp' method='post'>");
        out.println("<table border='0'>");
        // Categorías
        out.println("<tr>");
        out.println("<td>Categor&iacute;a</td>");
        out.println("<td>");
        out.println("<select name='idCategoria' onchange='javascript:document.forms[0].submit()'>");
        List rsCat = Sql.consulta("SELECT idCategoria,categoria FROM categorias");

        for (int fil = 1; fil < rsCat.size(); ++fil) {
            Object[] fila = (Object[]) rsCat.get(fil);

            if (idCategoria != null) {
                if (idCategoria.compareTo(fila[0].toString()) == 0) {
                    out.println("<option value='" + fila[0] + "' selected>" + fila[1] + "</option>");
                } else {
                    out.println("<option value='" + fila[0] + "'>" + fila[1] + "</option>");
                }
            } else {
                out.println("<option value='" + fila[0] + "'>" + fila[1] + "</option>");
                if (fil == 1) {
                    idCategoria = fila[0].toString();
                }
            }
        }
        out.println("</select>");
        out.println("</td>");
        out.println("</tr>");
        // CDs
        out.println("<tr>");
        out.println("<td>CD</td>");
        out.println("<td>");
        out.println("<select name='idCd' onchange='javascript:document.forms[0].submit()'>");
        List rsCd = Sql.consulta("SELECT idCd, titulo FROM cds WHERE idCategoria=" + idCategoria);

        String idCd2 = null;
        for (int fil = 1; fil < rsCd.size(); ++fil) {
            Object[] fila = (Object[]) rsCd.get(fil);

            if (idCd != null) {
                if (idCd.compareTo(fila[0].toString()) == 0) {
                    out.println("<option value='" + fila[0] + "' selected>" + fila[1] + "</option>");
                    idCd2 = fila[0].toString();
                } else {
                    out.println("<option value='" + fila[0] + "'>" + fila[1] + "</option>");
                    if (fil == 1) {
                        idCd2 = fila[0].toString();
                    }
                }
            } else {
                out.println("<option value='" + fila[0] + "'>" + fila[1] + "</option>");
                if (fil == 1) {
                    idCd2 = fila[0].toString();
                }
            }
        }
        idCd = idCd2;
        out.println("</select></td></tr></table></form>");

        // Los temas
        List rsTem = Sql.consulta("SELECT tema,interprete,compositor,duracion FROM temas WHERE idCd=" +
                idCd);

        out.println("<table border='2' width='90%'>");
        int seg = 0;
        int fil = 0;
        for (; fil < rsTem.size(); ++fil) {
            out.println("<tr bgcolor=" + ((fil % 2) == 1 ? "LightGreen" : "Cyan") + ">");

            Object[] fila = (Object[]) rsTem.get(fil);

            if (fil > 0) { // en 0 está los titulos
                seg += Integer.valueOf(fila[fila.length - 1].toString());	// tomando la duración
            }

            for (int col = 0; col < fila.length; ++col) {
                if (fil == 0) {	// los títulos
                    if (col == (fila.length - 1)) {
                        out.println("<th>" + fila[col] + "</th>");
                    } else {
                        out.println("<th>" + fila[col] + "</th>");
                    }
                } else {    // data
                    Object str = fila[col];
                    if (str == null) {
                        str = "";
                    }
                    if (col == (fila.length - 1)) {
                        out.println("<td align='right'>" + (str.toString().length() != 0 ? str.toString()
                                : "&nbsp;") + "</td>");
                    } else {
                        out.println("<td>" + (str.toString().length() != 0 ? str : "&nbsp;") + "</td>");
                    }
                }
            }
            out.println("</tr>");
        }
        out.println("<tr bgcolor=" + ((fil % 2) == 1 ? "LightGreen" : "Cyan") + ">");
        out.println("<td colspan='3' align='right'>Duraci&oacute;n del CD --></td><td align='right'>" +
                seg / 60 + "\' " + seg % 60 + "\"" + "</td>");
        out.println("</tr>");
        out.println("</table>");
        %>

        <p class="center">
            <a href="/musica/portada.html">Home</a>
        </p>
    </body>
</html>


